﻿Imports System.Data
Imports System.Data.Odbc
Imports System.Text
Imports System.Collections.Generic
Imports WSEntidades
Imports WSFactory

Public Class BeneficiariosDAL
    Public Function registraBeneficiarios(ByVal pBeneficiario As BeneficiariosBE) As Integer
        Dim instrSql As String
        Dim oInsParm(0 To 3) As OdbcParameter
        Try

            instrSql = "INSERT INTO ""Beneficiarios""(""IdBene"", ""NomBene"", ""Status"" " & _
                                        "VALUES (?,?,?);"

            oInsParm(0) = New OdbcParameter("", pBeneficiario.IdBene)
            oInsParm(1) = New OdbcParameter("", pBeneficiario.NomBene)
            oInsParm(2) = New OdbcParameter("", pBeneficiario.Status)

            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oInsParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
        Catch ex As Exception
            Throw
        Finally
            oInsParm = Nothing
            instrSql = Nothing
        End Try
    End Function

    Public Function modificaBeneficiario(ByVal pBeneficiario As BeneficiariosBE) As Boolean
        Dim instrsql As String
        Dim oUpdParm(0 To 2) As OdbcParameter
        Try
            instrsql = "UPDATE ""Beneficiarios"" " & _
                                        "SET ""NomBene"" = ?, " & _
                                                """Status"" = ? " & _
                                                "WHERE ""IdBene"" = ? "

            oUpdParm(0) = New OdbcParameter("", pBeneficiario.NomBene)
            oUpdParm(1) = New OdbcParameter("", pBeneficiario.Status)
            oUpdParm(2) = New OdbcParameter("", pBeneficiario.IdBene)
            'Crea el objeto connection
            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrsql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.AddRange(oUpdParm)
                    myConn.Open()
                    myComm.ExecuteNonQuery()
                    If (myComm.ExecuteNonQuery() > 0) Then
                        Return True
                    Else
                        Return False
                    End If
                End Using
            End Using
           
        Catch ex As Exception
            Throw
        Finally
            oUpdParm = Nothing
            instrsql = Nothing
        End Try
    End Function

    Public Function getBeneficiarioById(ByVal pId As Integer) As BeneficiariosBE
        Dim instrSql As String
        Dim tmpBene As BeneficiariosBE
        Try
            instrSql = "SELECT ""IdBene"", ""NomBene"", ""Status"" " & _
                                     "FROM ""Beneficiarios"" " & _
                                    "WHERE ""IdBene"" = ? ;"

            Using myConn As OdbcConnection = New OdbcConnection(My.Settings.cs)
                Using myComm As New OdbcCommand(instrSql, myConn)
                    myComm.CommandType = CommandType.Text
                    myComm.Parameters.Add(New OdbcParameter("", pId))
                    myConn.Open()
                    Using myDR As OdbcDataReader = myComm.ExecuteReader()
                        If (myDR.HasRows()) Then
                            myDR.Read()
                            tmpBene = beneficiariosFact.Build(myDR)
                        Else
                            tmpBene = Nothing
                        End If
                    End Using
                End Using
            End Using
            Return tmpBene
            
        Catch ex As Exception
            Throw
        Finally
            tmpBene = Nothing
            instrSql = ""
        End Try
    End Function

    Public Function existeBeneficiario(ByVal pBeneficiario As String) As Boolean
        Dim existe As Boolean = False
        Dim instrSql As String
        Dim oSelParm(0 To 0) As OdbcParameter
        Try

            instrSql = "SELECT COUNT(*) " & _
                                    "FROM ""Beneficiarios"" " & _
                                    "WHERE ""NomBene"" = ? ;"

            oSelParm(0) = New OdbcParameter("", pBeneficiario)
            Dim count As Object = db.ExecScalar(instrSql, CommandType.Text, oSelParm, My.Settings.cs)
            If (Not count Is Nothing) Then
                If (CShort(count) > 0) Then
                    existe = True
                Else
                    existe = False
                End If
            Else
                existe = False
            End If
            Return existe
        Catch ex As Exception
            Throw
        Finally
            instrSql = Nothing
            existe = Nothing
            oSelParm = Nothing
        End Try
    End Function
End Class
